package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.studygrades.StudyGradesAddDto;
import com.teacher.pojo.dto.studygrades.StudyGradesGetDto;
import com.teacher.pojo.dto.studygrades.StudyGradesUpdateDto;
import com.teacher.pojo.entity.StudyGrades;
import com.teacher.pojo.vo.studygrades.StudyGradesGetVo;
import com.teacher.service.IStudyGradesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 学业成绩 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/pc/study-grades")
@Api(tags = "学业成绩")
public class StudyGradesController {

    @Autowired
    private IStudyGradesService iStudyGradesService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加学业成绩
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加学业成绩 studyGrades_add")
    @PostMapping
    @PreAuthorize("hasAuthority('studyGrades_add')")
    public CommonResult<String> add(@RequestBody @Validated StudyGradesAddDto dto) {
        StudyGrades studyGrades = new StudyGrades();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, studyGrades);
        if (iStudyGradesService.save(studyGrades)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新学业成绩
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新学业成绩 studyGrades_update")
    @PutMapping
    @PreAuthorize("hasAuthority('studyGrades_update')")
    public CommonResult<String> update(@RequestBody @Validated StudyGradesUpdateDto dto) {
        StudyGrades studyGrades = new StudyGrades();
        BeanUtils.copyProperties(dto, studyGrades);
        if (iStudyGradesService.updateById(studyGrades)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除学业成绩
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除学业成绩 studyGrades_delete")
    @PreAuthorize("hasAuthority('studyGrades_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iStudyGradesService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询学业成绩
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询学业成绩 studyGrades_get")
    @PreAuthorize("hasAuthority('studyGrades_get')")
    public CommonResult<IPage<StudyGradesGetVo>> getPage(StudyGradesGetDto dto) {
        return iStudyGradesService.getPage(dto);
    }

    /**
     * 导入学业成绩
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入学业成绩 studyGrades_upload")
    @PreAuthorize("hasAuthority('studyGrades_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iStudyGradesService.upload(file);
    }

}

